package cn.kgc.service.impl;

import cn.kgc.dao.StudentDao;
import cn.kgc.dao.impl.StudentDaoImpl;
import cn.kgc.entity.Student;
import cn.kgc.service.StudentService;
import cn.kgc.util.Constands;
import cn.kgc.util.Page;

import java.sql.SQLException;
import java.util.List;

public class StudentServiceImpl implements StudentService {
    private StudentDao studentDao = new StudentDaoImpl();

    @Override
    public Page getPageInfo(int pageNo) throws SQLException {
        Page page = new Page();

        int countNo = studentDao.getCount();
        List<Student> studentList = studentDao.selectStudentPage((pageNo - 1) * Constands.PAGE_SIZE);

        page.setCountNo(countNo);
        page.setStudentList(studentList);
        page.setCurrentPageNo(pageNo);

        return page;
    }

    @Override
    public boolean delStudent(String studentNo) {
        int i = studentDao.delStudentByStudentNo(Integer.parseInt(studentNo));
        if (i == 1) {
            return true;
        }
        return false;
    }

    @Override
    public boolean insertStudent(Student student) {
        int i = studentDao.insertStudent(student);
        if (i == 1) {
            return true;
        }
        return false;
    }

    @Override
    public Student select(String studentNo) throws SQLException {
        return studentDao.select(Integer.parseInt(studentNo));
    }

    @Override
    public boolean update(Student student) {
        int line = studentDao.update(student);
        if (line > 0) {
            return true;
        }
        return false;
    }
}
